package edu.nf.webapp.controller;

import com.google.gson.GsonBuilder;
import edu.nf.webapp.common.result.ResultData;
import edu.nf.webapp.entity.User;
import edu.nf.webapp.service.LoginService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import org.nf.web.annotation.Model;
import org.nf.web.annotation.RequestMapping;
import org.nf.web.annotation.RequestParam;
import org.nf.web.servlet.View;
import org.nf.web.view.ForwardyView;
import org.nf.web.view.JsonView;
import org.nf.web.view.RedirectView;

import java.util.Date;

/**
 * @Author lzp
 * @Date 2024-05-16
 */
@RequestMapping("/user")
public class MethodContrller {
    @RequestMapping("/login")
    public View login(@RequestParam("account") String account,
                      @RequestParam("password") String password) {
        LoginService service = new LoginService();
        ResultData<User> data = new ResultData();
        User user = service.auth(account, password);
        data.setCode(200);
        data.setData(user);
        data.setMessage("Login success");
        return new JsonView(data);
    }

    @RequestMapping("/reg")
    public void reg(HttpServletRequest req, HttpServletResponse resp){
        System.out.println("注册");
    }

    @RequestMapping("/add")
    public void add(HttpServletRequest req){
        System.out.println(req);
        System.out.println("请求");
    }
    @RequestMapping("/del")
    public void del(HttpServletResponse resp){
        System.out.println(resp);
        System.out.println("响应");
    }
    @RequestMapping("/sel")
    public void sel(HttpSession session){
        System.out.println(session);
        System.out.println("session");
    }

    @RequestMapping("/add2")
    public void add2(@RequestParam("name") String name,
                     @RequestParam("age") int age){
        System.out.println(name +" : " +age);
    }

    @RequestMapping("/user")
    public void add3(@Model()User user){
        System.out.println(user.getUserName());
        System.out.println(user.getAge());
    }

    @RequestMapping("/redirect")
    public View redirect(){
        return new RedirectView("/index.html");
    }

    /**
     * 转发
     * @return
     */
    @RequestMapping("/forward")
    public View forward(){
        ForwardyView fv =new  ForwardyView("/index.jsp");
        fv.addObject("name","user");
        return fv;
    }

    @RequestMapping("/test")
    public String test(){
       return "helloword";
    }

    @RequestMapping("/json")
    public View toJson(){
        User user = new User();
        user.setUserName("zhangsan");
        user.setAge(22);
        user.setBirth(new Date());
        JsonView view = new JsonView(user);
        view.format("yyyy-MM-dd");
        return view;
    }


}
